package com.why.zing.payapi.service.impl;

import com.alibaba.fastjson.JSON;
import com.why.zing.payapi.constant.JobTypeEnum;
import com.why.zing.payapi.constant.PayCodeEnum;
import com.why.zing.payapi.dao.PayOrderExtJobMapper;
import com.why.zing.payapi.entity.PayOrderExtJob;
import com.why.zing.payapi.exception.PayException;
import com.why.zing.payapi.feignclient.DelayQueueFeign;
import com.why.zing.payapi.model.ClosePayOrderRequest;
import com.why.zing.payapi.model.delay.Job;
import com.why.zing.payapi.model.delay.JobDie;
import com.why.zing.payapi.service.JobService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * Job管理
 */
@Service
public class JobServiceImpl implements JobService {

    @Value("${spring.application.name}")
    private String appName;

    @Value("${callBackUrl.closePayOrder}")
    private String closePayOrderCallBackUrl;

    @Autowired
    private DelayQueueFeign delayQueueFeign;

    @Autowired
    private PayOrderExtJobMapper jobMapper;


    @Override
    public void addExpireOrderJob(String orderNo, Date expireTime) {
        Job job = new Job();
        String jobId = UUID.randomUUID().toString();
        job.setJobId(jobId);
        job.setTopic(appName);
        job.setDelay(expireTime.getTime());
        job.setUrl(closePayOrderCallBackUrl);
        job.setBody(JSON.toJSONString(new ClosePayOrderRequest(orderNo)));
        delayQueueFeign.addJob(job);

        PayOrderExtJob extJob = new PayOrderExtJob();
        extJob.setJobId(jobId);
        extJob.setOrderNo(orderNo);
        extJob.setJobType(JobTypeEnum.EXPIRE_CLOSE_ORDER.getCode());
        jobMapper.insertSelective(extJob);
    }


    @Override
    public void addNotifyOrderJob(String orderNo, String callbackUrl, String body) {
        Job job = new Job();
        String jobId = UUID.randomUUID().toString();
        job.setJobId(jobId);
        job.setTopic(appName);
        job.setDelay(System.currentTimeMillis());
        job.setUrl(callbackUrl);
        job.setBody(body);
        delayQueueFeign.addJob(job);

        PayOrderExtJob extJob = new PayOrderExtJob();
        extJob.setJobId(jobId);
        extJob.setOrderNo(orderNo);
        extJob.setJobType(JobTypeEnum.NOTIFY.getCode());
        jobMapper.insertSelective(extJob);
    }

    /**
     * 删除job
     *
     * @param jobType 订单类型
     * @param orderNo 订单号
     */
    @Override
    public void deleteJob(String jobType, String orderNo) {

        PayOrderExtJob payOrderExtJob = new PayOrderExtJob();
        payOrderExtJob.setOrderNo(orderNo);
        payOrderExtJob.setFlag(true);
        payOrderExtJob.setJobType(jobType);
        List<PayOrderExtJob> payOrderExtJobs = jobMapper.select(payOrderExtJob);
        if(CollectionUtils.isEmpty(payOrderExtJobs)){
            throw new PayException(PayCodeEnum.PAY_ORDER_JOB_NOT_EXIST);
        }

        PayOrderExtJob orderExtJob = payOrderExtJobs.get(0);
        JobDie jobDie = new JobDie();
        jobDie.setJobId(orderExtJob.getJobId());
        jobDie.setTopic(appName);
        delayQueueFeign.deleteJob(jobDie);

        payOrderExtJob.setFlag(false);
        jobMapper.updateByPrimaryKeySelective(payOrderExtJob);
    }
}
